package com.chi.controller;

import com.chi.pojo.Pwd;
import com.chi.service.PwdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/Pwd")
public class PwdController {

    @Autowired
    @Qualifier("pwdServiceImpl")
    private PwdService pwdService;

    @GetMapping("/getPwdPage")
    public String getPwdPage(@RequestParam("startIndex") int startIndex, Model model){
        startIndex = (startIndex - 1 ) * 10;
        List<Pwd> list = pwdService.queryPwdList(startIndex,10);
        model.addAttribute("list",list);
        return "allPwd";
    }

    @GetMapping("/getPwdList")
    public String getPwdList(Model model){
        List<Pwd> list = pwdService.queryPwdList(0,10);
        model.addAttribute("list",list);
        return "allPwd";
    }

    @GetMapping("/deletePwd")
    public String deletePwd(@RequestParam("id") int id){
        pwdService.deletePwdById(id);
        return "redirect:/Pwd/getPwdList";
    }

    @PostMapping("/updatePwd")
    public String updatePwd(Pwd pwd){
        pwdService.updatePwd(pwd);
        return "redirect:/Pwd/getPwdList";
    }

    @GetMapping("/toUpdatePwd")
    public String toUpdatePwd(int id,Model model){
        Pwd pwd = pwdService.queryPwdById(id);
        model.addAttribute("pwd",pwd);
        return "updatePwd";
    }

    @PostMapping ("/addPwd")
    public String addPwd(Pwd pwd){
        pwdService.addPwd(pwd);
        return "redirect:/Pwd/getPwdList";
    }

    @GetMapping("/toAddPwd")
    public String toAddPwd(){
        return "addPwd";
    }

    @GetMapping("/selectPwd")
    public String selectPwd(String accountName,Model model){
        List<Pwd> list = pwdService.selectPwd(accountName);
        model.addAttribute("list",list);
        return "allPwd";
    }

    @GetMapping("/getPwdNum")
    @ResponseBody
    public String getPwdNum(){
        int pwdNum = pwdService.getPwdNum();
        return String.valueOf(pwdNum);
    }
}
